<template>
  <div class="new-purchase-layout">
    <crumbList :navList="navList"></crumbList>
    <div class="new-layout">
      <div class="new-content">
        <el-form :model="newBuild" :rules="newRules" ref="newBuild" label-width="110px" class="demo-ruleForm">
          <h2 class="new-ititle">1.基本信息</h2>
          <el-form-item label="城市" prop="cityName">
            <el-select v-model="newBuild.cityName" placeholder="请选择" class="w280" disabled>
              <el-option :label="item.cityName" :value="item.cityName" v-for="(item,index) in cityMsg"
                         :key="index"></el-option>
            </el-select>
          </el-form-item>
          <div class="new-oneline clearfix">
            <el-form-item label="厂商" prop="factoryName" class="mr fl">
              <el-select v-model="newBuild.factoryName" placeholder="请选择" class="w280" disabled>
                <el-option :label="item.factoryName" :value="item.factoryName" v-for="(item,index) in factoryMsg"
                           :key="index"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <h2 class="new-ititle">2.回收详情</h2>
          <div class="new-oneline clearfix">
            <el-form-item label="机具固号" prop="fixedNumber" class="mr fl">
              <el-input v-model="newBuild.fixedNumber" placeholder="请输入" class="w280"></el-input>
            </el-form-item>
            <el-form-item label="交接人" prop="recoverMan" class="fl">
              <el-input v-model="newBuild.recoverMan" placeholder="交接人"></el-input>
            </el-form-item>
          </div>
          <div class="new-oneline clearfix">
            <el-form-item label="回收原因" prop="recoverReason" class="mr fl">
              <el-input v-model="newBuild.recoverReason" placeholder="请输入" class="w280"></el-input>
            </el-form-item>
            <el-form-item label="负责人" prop="chargeMan" class="fl">
              <el-input v-model="newBuild.chargeMan" placeholder="负责人"></el-input>
            </el-form-item>
          </div>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="newBuild.remark" class="w280 h120" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item class="new-submit">
            <el-button class="new-btn-no buleSpaceBtn" @click="resetForm">取消</el-button>
            <el-button class="new-btn-ok buleBtn" @click="submitForm('newBuild')">完成</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
  import crumbList from '../../common/crumbList.vue'
  import ajax from '../../../utils/ajax/index'
  import getParam from '../../../utils/getParams/getParams'
  import { mapState } from 'vuex'

  export default {
    data () {
      return {
        navList: [
          {name: '项目详情', path: ''},
          {name: '机具回收', path: 'recycle'},
          {
            name: JSON.parse(sessionStorage.getItem('projectItemData')).name,
            path: 'recycleList?id=' + getParam('id')
          },
          {
            name: JSON.parse(sessionStorage.getItem('recycle')) ? '修改回收信息' : '新增回收信息',
            path: 'newRecycle?id=' + getParam('id')
          }
        ],
        newBuild: {
          cityName: JSON.parse(sessionStorage.getItem('projectItemData')).cityName,
          factoryName: JSON.parse(sessionStorage.getItem('projectItemData')).factoryName,
          fixedNumber: '',
          recoverMan: '',
          recoverReason: '',
          chargeMan: '',
          remark: ''
        },
        newRules: {
          cityName: [
            {required: true, message: '请选择城市', trigger: 'change'}
          ],
          factoryName: [
            {required: true, message: '请选择厂商', trigger: 'change'}
          ],
          fixedNumber: [
            {required: true, message: '请输入机具固号', trigger: 'blur'}
          ],
          recoverMan: [
            {required: true, message: '请输入交接人', trigger: 'blur'}
          ],
          successor: [
            {required: true, message: '请输入安装人', trigger: 'blur'}
          ],
          recoverReason: [
            {required: true, message: '请输入回收原因', trigger: 'blur'}
          ],
          chargeMan: [
            {required: true, message: '请输入负责人', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '请输入备注', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      ...mapState(['cityMsg', 'factoryMsg'])
    },
    components: {crumbList},
    created () {
      let itemData = JSON.parse(sessionStorage.getItem('recycle'))
      if (itemData) {
        this.newBuild = itemData
      }
    },
    methods: {
      // 重置数据
      resetForm () {
        this.$router.push('/recycleList?id=' + getParam('id'))
        /* let itemData = JSON.parse(sessionStorage.getItem('recycle'))
        if (itemData) {
          this.newBuild = itemData
        } else {
          this.newBuild = {
            cityName: '',
            factoryName: '',
            fixedNumber: '',
            recoverMan: '',
            recoverReason: '',
            chargeMan: '',
            remark: ''
          }
        } */
      },
      // 提交保存数据
      submitForm (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.saveNewData()
          } else {
            this.$message.error('您有相关数据未填写')
            return false
          }
        })
      },
      saveNewData () {
        let params = this.newBuild
        params.projectCode = getParam('id')
        let itemData = JSON.parse(sessionStorage.getItem('recycle'))
        if (itemData) {
          params.id = itemData.id
        }
        let url = this.api2.recycle.addProjectData
        ajax.post(url, params).then((res) => {
          if (res.msg.code === '20000') {
            this.$message({message: res.msg.message, type: 'success'})
            this.$router.push('/recycleList?id=' + getParam('id'))
          } else {
            this.$message.error(res.msg.message)
          }
        })
      }
    }
  }
</script>
